/*
 * @Author: xuesong
 * @Date: 2021-12-14 22:19:25
 * @LastEditors: xuesong
 * @LastEditTime: 2022-03-05 13:33:28
 * @FilePath: \BLE-SDK-SVN499-V03 - 副本\projects\ble_mulit\simpleBleMultiConnection\src\TM1640\tm1640.c
 */
#include "sdk_config.h"
#include "clock.h"
#include "log.h"
#include "OSAL.h"
#include "gpio.h"
#include "tm1640.h"

#define  TM_POWER_PIN       GPIO_P31
#define  TM_DIO_PIN         GPIO_P33
#define  TM_CLK_PIN         GPIO_P32

uint8 TM1640_DISP_BUFF[TM1640_DISPLAY_SIZE];

void Tm1640_IO_PowerDown(void)
{
    TM1640_Init();
    WaitUs(10);
    HalGpioSet(TM_DIO_PIN,Bit_DISABLE);
    HalGpioPinInit(TM_DIO_PIN, GPIO_OUTPUT);
    HalGpioPupdConfig(TM_DIO_PIN,GPIO_PULL_DOWN);
    HalGpioSet(TM_CLK_PIN,Bit_DISABLE);
    HalGpioPinInit(TM_CLK_PIN, GPIO_OUTPUT);   
    HalGpioPupdConfig(TM_CLK_PIN,GPIO_PULL_DOWN);   
    HalGpioSet(TM_POWER_PIN,Bit_ENABLE);
}

void Tm1640_IO_Init(void)
{
    HalGpioSet(TM_POWER_PIN,Bit_DISABLE);
    HalGpioPinInit(TM_POWER_PIN, GPIO_OUTPUT);
    WaitUs(10);  
    HalGpioSet(TM_DIO_PIN,Bit_ENABLE);
    HalGpioPinInit(TM_DIO_PIN, GPIO_OUTPUT);
    HalGpioPupdConfig(TM_DIO_PIN,GPIO_PULL_UP_S);
    HalGpioSet(TM_CLK_PIN,Bit_ENABLE);
    HalGpioPinInit(TM_CLK_PIN, GPIO_OUTPUT);   
    HalGpioPupdConfig(TM_CLK_PIN,GPIO_PULL_UP_S);
}

void TM1640_Init(void)
{
    uint8    TM_DataBuffer[TM1640_DISPLAY_SIZE];
    Tm1640_IO_Init();
    WaitUs(10);
    for(uint8 i = 0; i<TM1640_DISPLAY_SIZE; i++)
        TM_DataBuffer[i] = 0;
    TM_Display(TM_DataBuffer,TM1640_DISPLAY_SIZE);
}


static void TM_Send8bit(uint8 data)
{
    for(uint8 i = 0; i < 8 ; i++){
        HalGpioSet(TM_CLK_PIN,Bit_DISABLE);     //CLK = 0
        if(data&0x01)
            HalGpioSet(TM_DIO_PIN,Bit_ENABLE);  //DIO = 1
        else
            HalGpioSet(TM_DIO_PIN,Bit_DISABLE); //DIO = 0
        WaitUs(1);
        HalGpioSet(TM_CLK_PIN,Bit_ENABLE);      //CLK = 1
        WaitUs(1);
        data >>= 1;
    }
    HalGpioSet(TM_CLK_PIN,Bit_DISABLE);         //CLK = 0
    // HalGpioSet(TM_DIO_PIN,Bit_DISABLE);         //DIO = 0
}

static void TM_SendCmd(uint8 cmd)
{
    HalGpioSet(TM_CLK_PIN,Bit_ENABLE);          //CLK = 1
    HalGpioSet(TM_DIO_PIN,Bit_ENABLE);          //DIO = 1
    WaitUs(1);
    HalGpioSet(TM_DIO_PIN,Bit_DISABLE);         //DIO = 0
    TM_Send8bit(cmd);
    WaitUs(2);
}

static void TM_SendEnd(void)
{
    HalGpioSet(TM_DIO_PIN,Bit_DISABLE);         //DIO = 0
    HalGpioSet(TM_CLK_PIN,Bit_ENABLE);          //CLK = 1
    WaitUs(1);
    HalGpioSet(TM_DIO_PIN,Bit_ENABLE);         //DIO = 0
    WaitUs(2);
}


void TM_Display(uint8 *dataBuff,uint8 len)
{
    TM_SendCmd(DISPLAY_DATA_CMD|DISPLAY_DATA_ADDP);
    TM_SendEnd();
    TM_SendCmd(DISPLAY_ADDR_CMD);  //设置显示命令,写数据自动加
    if(len > TM1640_DISPLAY_SIZE)
        len = TM1640_DISPLAY_SIZE;
    for(uint8 i = 0; i<len ;i++)
    {
        TM_Send8bit((*dataBuff)&0xFF);
        dataBuff++;
    }
    TM_SendEnd();
    TM_SendCmd(DISPLAY_CONTROL_CMD|DISPLAY_CONTROL_ON|DISPLAY_CONTROL_PWM14_16);
    TM_SendEnd();
}


